var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
var DEFAULT_PATTERN = 'yyyy-MM-dd';

function padding(s, len) {
    var len = len - (s + '').length;
    for (var i = 0; i < len; i++) { s = '0' + s; }
    return s;
};

export default {
    getQueryStringByName: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        var context = "";
        if (r != null)
            context = r[2];
        reg = null;
        r = null;
        return context == null || context == "" || context == "undefined" ? "" : context;
    },
    isvalidPhone: function (str) {
        var reg=11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
        return reg.test(str)
    },
    isvalidEmail: function (str) {
        var regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
        // var regEmail = /^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/;
        return regEmail.test(str)
    },
    getCurrentIp:function () {

    },
    /**
     * 获取uuid
     */
     getUUID:function () {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
            return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
        })
    },


    formatDate: {
        format: function (date, pattern) {
            pattern = pattern || DEFAULT_PATTERN;
            return pattern.replace(SIGN_REGEXP, function ($0) {
                switch ($0.charAt(0)) {
                    case 'y': return padding(date.getFullYear(), $0.length);
                    case 'M': return padding(date.getMonth() + 1, $0.length);
                    case 'd': return padding(date.getDate(), $0.length);
                    case 'w': return date.getDay() + 1;
                    case 'h': return padding(date.getHours(), $0.length);
                    case 'm': return padding(date.getMinutes(), $0.length);
                    case 's': return padding(date.getSeconds(), $0.length);
                }
            });
        },
        parse: function (dateString, pattern) {
            var matchs1 = pattern.match(SIGN_REGEXP);
            var matchs2 = dateString.match(/(\d)+/g);
            if (matchs1.length == matchs2.length) {
                var _date = new Date(1970, 0, 1);
                for (var i = 0; i < matchs1.length; i++) {
                    var _int = parseInt(matchs2[i]);
                    var sign = matchs1[i];
                    switch (sign.charAt(0)) {
                        case 'y': _date.setFullYear(_int); break;
                        case 'M': _date.setMonth(_int - 1); break;
                        case 'd': _date.setDate(_int); break;
                        case 'h': _date.setHours(_int); break;
                        case 'm': _date.setMinutes(_int); break;
                        case 's': _date.setSeconds(_int); break;
                    }
                }
                return _date;
            }
            return null;
        }

    },
     /**
     * 树形数据转换
     * @param {*} data
     * @param {*} id
     * @param {*} pid
     */
    treeDataTranslate (data, id = 'id', pid = 'parentId') {
        var res = []
        var temp = {}
        for (var i = 0; i < data.length; i++) {
            temp[data[i][id]] = data[i]
        }
        for (var k = 0; k < data.length; k++) {
            if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
                if (!temp[data[k][pid]]['children']) {
                    temp[data[k][pid]]['children'] = []
                }
                if (!temp[data[k][pid]]['_level']) {
                    temp[data[k][pid]]['_level'] = 1
                }
                data[k]['_level'] = temp[data[k][pid]]._level + 1
                 temp[data[k][pid]]['children'].push(data[k])
            } else {
                res.push(data[k])
            }
        }
        return res
    },

    /**
     * 树形数据转换
     * @param {*} data
     * @param {*} id
     * @param {*} pid
     */
     treeDataTranslateMenu (data, id = 'id', pid = 'parentId') {
        var res = []
        var temp = {}
        for (var i = 0; i < data.length; i++) {
            temp[data[i][id]] = data[i]
        }
        for (var k = 0; k < data.length; k++) {
            if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
                if (!temp[data[k][pid]]['children']) {
                    temp[data[k][pid]]['children'] = []
                }
                if (!temp[data[k][pid]]['_level']) {
                    temp[data[k][pid]]['_level'] = 1
                }
                data[k]['_level'] = temp[data[k][pid]]._level + 1
                if (data[k]['_level'] <= 3) {
                    temp[data[k][pid]]['children'].push(data[k])
                }
            } else {
                res.push(data[k])
            }
        }
        return res
    },
    /**
     * 树形数据转换
     * @param {*} data
     * @param {*} id
     * @param {*} pid
     */
    treeDataTranslateForMenu (data, id = 'id', pid = 'parentId') {
        var res = []
        var temp = {}
        for (var i = 0; i < data.length; i++) {
            temp[data[i][id]] = data[i]
        }
        for (var k = 0; k < data.length; k++) {
            if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {

                if (!temp[data[k][pid]]['children']) {
                    temp[data[k][pid]]['children'] = [];
                }
                if (!temp[data[k][pid]]['_level']) {
                    temp[data[k][pid]]['_level'] = 1
                }
                data[k]['_level'] = temp[data[k][pid]]._level + 1
                temp[data[k][pid]]['children'].push(data[k])
            } else {
                res.push(data[k])
            }
        }
        return res
    },

// 通用下载方法
      download(fileName) {
        window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
    },

// 字符串格式化(%s )
    sprintf(str) {
        var args = arguments, flag = true, i = 1;
        str = str.replace(/%s/g, function () {
            var arg = args[i++];
            if (typeof arg === 'undefined') {
                flag = false;
                return '';
            }
            return arg;
        });
        return flag ? str : '';
    },

// 转换字符串，undefined,null等转化为""
    praseStrEmpty(str) {
        if (!str || str == "undefined" || str == "null") {
            return "";
        }
        return str;
    },

};
